package jezyk_java.robert_wolos.watki;


/**
 * Autor: Daniel Michalski
 * Data: 11.07.13
 */

public class MyRunnable implements Runnable {
    private int id;
    private int i;
    private boolean condition;

    public MyRunnable(int id) {
        this.id = id;
        this.i = 1;
        condition = true;
    }

    @Override
    public void run() {
        while (true) {
            if (id == 1 && i == 10)
                condition = false;
            try {
                synchronized (this) {
                    if (!condition) {
                        doCriticalThing();
                        wait();
                    }
                }

                System.out.println("watek " + id + ", i = " + i);
                Thread.sleep(200);
            } catch (InterruptedException e) {
                System.out.println("Przerwano wątek");
            }
            i++;
        }
    }

    private void doCriticalThing() {
        System.out.println("This is critical");
    }
}
